package com.khh.base.util;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.apache.log4j.Logger;

import com.khh.base.assertion.AssertValue;
public class StringUtil implements Serializable {
	private static Logger logger = Logger.getLogger(StringUtil.class);
	private static final long serialVersionUID = 1L;
	static final byte[] HEX_CHAR_TABLE = { (byte) '0', (byte) '1', (byte) '2',
			(byte) '3', (byte) '4', (byte) '5', (byte) '6', (byte) '7',
			(byte) '8', (byte) '9', (byte) 'a', (byte) 'b', (byte) 'c',
			(byte) 'd', (byte) 'e', (byte) 'f' };
	private StringUtil() {
		// hide default constructor to prevent instantiation
	}
	public static String toHexString(byte[] bytes) {
		try {
			byte[] hex = new byte[2 * bytes.length];
			int index = 0;
			for (int i = 0; i < bytes.length; i++) {
				byte b = bytes[i];
				int v = b & 0xFF;
				hex[index++] = HEX_CHAR_TABLE[v >>> 4];
				hex[index++] = HEX_CHAR_TABLE[v & 0xF];
			}
			return new String(hex, "US-ASCII");
		} catch (UnsupportedEncodingException e) {
			// should not happen, US-ASCII is a standard charset
			throw new AssertionError(e);
		}
	}
	public static String toHexStringHibernate(byte[] bytes) {
		StringBuffer buf = new StringBuffer();
		for (int i = 0; i < bytes.length; i++) {
			String hexStr = Integer.toHexString(bytes[i] - Byte.MIN_VALUE);
			if (hexStr.length() == 1)
				buf.append('0');
			buf.append(hexStr);
		}
		return buf.toString();
	}
	public static String byteToString(byte[] bytes) {
		try {
			return new String(bytes, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			logger.error("byte[]转换到String错误", e);
			return null;
		}
	}
	public static byte[] stringToBytes(String value) {
		try {
			return value.getBytes("UTF-8");
		} catch (UnsupportedEncodingException e) {
			logger.error("String转换到错误byte[]", e);
			e.printStackTrace();
			return null;
		}
	}
	/***
	 * 将字符转换为 Properties,字符串格式,key1=value1;key2=value2;key3=value3
	 * 
	 * @param expression
	 * @return
	 */
	public static Properties toProperties(String expression) {
		Properties p = new Properties();
		if (!AssertValue.isNotNullAndNotEmpty(expression))
			return p;
		String[] params = expression.split(";");
		for (int i = 0; i < params.length; i++) {
			String[] param = params[i].split("=");
			if (param.length >= 2) {
				p.setProperty(param[0], param[1]);
			}
		}
		return p;
	}
	public static List<String> split(String source, int size) {
		List<String> tempList = new ArrayList<String>();
		if (!AssertValue.isNotNull(source)) {
			return tempList;
		}
		if (size >= source.length()) {
			size = source.length();
		}
		boolean state = true;
		while (state) {
			String tempString = source.substring(0, size);
			tempList.add(tempString);
			source = source.substring(size, source.length());
			if (source.length() <= size) {
				if (AssertValue.isNotNullAndNotEmpty(source)) {
					tempList.add(source);
				}
				state = false;
			}
		}
		return tempList;
	}
}